﻿--#include "data\config\activity\LivenessActivityConfig.lua" once

--协议操作
function OnLivenessActivity(sysarg, msgid, packet)
    local Index = DataPack.readByte(packet)
    --print("Index ="..Index)
    if Index == 0 then
        SendLivenessActivity(sysarg)
    else
        GetLivenessActivityAward(sysarg, Index)
    end
end

--获得活跃度
function UpdateLivenessActivityState(sysarg)
    local var = Actor.getStaticVar(sysarg)
    if not var.activityVal then
        var.activityVal = 0
    end
    --print("var.activityVal ="..var.activityVal)
    if not var.days then var.days = {}  end
    if not var.days.activityAwardState then
        var.days.activityAwardState = {}
    end
    for idx = 1, #LivenessActivityCfg.dailyActivityAwards do
        --print("idx = "..idx)
        local cfg = LivenessActivityCfg.dailyActivityAwards[idx]
        if cfg.ActivityNum <= var.activityVal and ( (not var.days.activityAwardState[idx])  or var.days.activityAwardState[idx] ~= 2 ) then
            var.days.activityAwardState[idx] = 1
        end
    end
    SendLivenessActivity(sysarg)
end

--发送数据
function SendLivenessActivity(sysarg)
    local var = Actor.getStaticVar(sysarg)
    if not var.activityVal then
        var.activityVal = 0
    end
    if not var.days then var.days = {}  end
    if not var.days.activityAwardState then
        var.days.activityAwardState = {}
    end
    local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sLivenessActivity)
    if not pack then return end
    local len = #LivenessActivityCfg.dailyActivityAwards
    DataPack.writeShort(pack, var.activityVal)
    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local state = var.days.activityAwardState[idx] or 0
        --print("state ="..state)
        DataPack.writeByte(pack, idx)
        DataPack.writeByte(pack, state)
    end
    DataPack.flush(pack)
end

--领取奖励
function GetLivenessActivityAward(sysarg, Index)
    local var = Actor.getStaticVar(sysarg)
    if not var.activityVal then
        var.activityVal = 0
    end
    if not var.days then var.days = {}  end
    if not var.days.activityAwardState then
        var.days.activityAwardState = {}
    end
    
    if Index > #LivenessActivityCfg.dailyActivityAwards then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.LivenessActivity001, ttFlyTip)
        return
    end

    local cfg = LivenessActivityCfg.dailyActivityAwards[Index]
    if not cfg then
        return 
    end

    if not cfg.awards then
        return
    end

    local days = System.getDaysSinceOpenServer()
    local rewardCfg = GetElemByCond(cfg.awards, days)
    if not rewardCfg then
        return
    end
    local rewards = rewardCfg.dailyAwards

    if not rewards then
        return
    end

    if (not var.days.activityAwardState[Index] or var.days.activityAwardState[Index] == 0) and cfg.ActivityNum > var.activityVal then
        Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.LivenessActivity005, cfg.ActivityNum), ttFlyTip)
        return
    end    

    if not ( (var.days.activityAwardState[Index] and  var.days.activityAwardState[Index] == 1) 
        or ((not var.days.activityAwardState[Index] or var.days.activityAwardState[Index] == 0) and cfg.ActivityNum <= var.activityVal)) then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.LivenessActivity002, ttFlyTip)
        return
    end

    var.days.activityAwardState[Index] = 2
    
    if IsBagGridEnough(sysarg, rewards) then
        GiveCommonAward(sysarg, rewards, LogIds.LivenessActivityAwardsLogId, Lang.LogTips.LivenessActivityAwards)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.LivenessActivity003
        local content = Lang.ScriptTips.LivenessActivity004

        local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.LivenessActivityAwardsLogId, Lang.LogTips.LivenessActivityAwards, vocation, sex)
    end

    SendLivenessActivity(sysarg)
end

function GmLivenessActivity(sysarg)
    local var = Actor.getStaticVar(sysarg)
    var.activityVal = LivenessActivityCfg.dailyActivityTotalNum
    UpdateLivenessActivityState(sysarg)
end


